package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Box;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class ConversionSkill extends BaseSkill {

    //public static UUID uuid = UUID.fromString("33CD15E9-53B6-5DDD-4411-B622231D45EC");

    public ConversionSkill(int level){super(level);}

    public ConversionSkill(NbtCompound nbtCompound){super(nbtCompound);}

    public ConversionSkill(){
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        return "自己受到的伤害转化为生命恢复与最大生命上限并平均分配给周围的友方生物";
    }

    public SkillRarity getSkillRarity(){return SkillRarity.Mystery;}

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if(!source.isOutOfWorld())
        {
            double L = 32.0d;
            List<MobEntity> mobEntitiyList = target.world.getEntitiesByClass(
                    MobEntity.class,
                    target.getBoundingBox().stretch(L, L / 2, L).stretch(-L, -L / 2, -L),
                    e -> e != target );
            Iterator iterator = mobEntitiyList.iterator();
            while (iterator.hasNext()){
                MobEntity mob = (MobEntity) iterator.next();
                EntityAttributeModifier modifier = new EntityAttributeModifier(
                        UUID.randomUUID(),
                        getName(),
                        (amount.getValue()/mobEntitiyList.size()),
                        EntityAttributeModifier.Operation.fromId(0));
                EntityAttributeInstance entityAttributeInstance = mob.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH);
                entityAttributeInstance.addPersistentModifier(modifier);

                mob.heal(amount.getValue()/mobEntitiyList.size());
            }
        }
        return true;
    }
}
